﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.ComponentModel;
using System.Linq;
using ScrumTable.BL.DM.DataManagement;
using ScrumTable.Common.Collections;
using ScrumTable.UI.ViewModel.Data;
using ScrumTable.UI.ViewModel.Dynamic;

#endregion

namespace ScrumTable.UI.ViewModel
{
    
    /// <summary>
    /// Bug for the View model
    /// </summary>
    public class BugViewModel : ViewModelItemBase<Bug>
    { 

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        /// <summary>
        /// Func for changing 
        /// </summary>
        public static readonly Func<object, ScrumState, CanDropResult> CanChangeState = CanChangeStateResult<BugViewModel>;

  

        /// <summary>
        /// Sorting property
        /// </summary>
        public override double? Sorting
        {
            get { return Ranking; }
        }

        /// <summary>
        /// Specifies the bug severity.
        /// </summary>
        public string Severity
        {
            get { return IsDisposed ? null : Original.Severity; }
            set
            {
                Original.Severity = value;
                SendPropertyChanged("Severity");
                SendPropertyChanged("SeverityIndex");
            }
        }


        /// <summary>
        /// Specifies the bug severity.
        /// </summary>
        public int? SeverityIndex
        {
            get
            {
                int index = Original.ItemType.FieldMeta.TryGetByName("Severity").AllowedValues.GetIndex(Severity);
                return index != -1 ? index+1 : (int?) null;
            }
        }

        /// <summary>
        /// Gets the parent as user story (null if the parent is not a user story).
        /// </summary>
        public UserStoryViewModel ParentAsStoryViewModel
        {

            get
            {
                if (Original.Parent is UserStory)
                {
                    return new UserStoryViewModel((UserStory) Original.Parent);
                }
                return null;
            }
        }


        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------
        /// <summary>
        /// Initializes a new instance of the <see cref="BugViewModel"/> class.
        /// </summary>
        /// <param name="orginal">The bug.</param>
        public BugViewModel(Bug orginal) : base(orginal)
        {
          
        }
        
      
        #endregion

        #region Overrides

        /// <summary>
        /// Determines whether the specified <see cref="System.Object"/> is equal to this instance.
        /// </summary>
        /// <param name="obj">The <see cref="System.Object"/> to compare with this instance.</param>
        /// <returns>
        /// 	<c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise, <c>false</c>.
        /// </returns>
        /// <exception cref="T:System.NullReferenceException">
        /// The <paramref name="obj"/> parameter is null.
        /// </exception>
        public override bool Equals(object obj)
        {
            if (obj is BugViewModel) return Equals(obj as BugViewModel);
            if (obj is Bug) return Original.Equals(obj);

            return false;
        }

        /// <summary>
        /// Equalses the specified other.
        /// </summary>
        /// <param name="other">The other.</param>
        /// <returns></returns>
        public bool Equals(BugViewModel other)
        {
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;
            return other.Id == Id; 
        }

        /// <summary>
        /// Returns a hash code for this instance.
        /// </summary>
        /// <returns>
        /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. 
        /// </returns>
        public override int GetHashCode()
        {
            return (Original != null ? Original.GetHashCode() : 0);
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------
        

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------


        protected override void OnOrginalPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Severity")
            {
                SendPropertyChanged("SeverityIndex");
            }
            base.OnOrginalPropertyChanged(sender, e);
        }
        #endregion
    }
}